<?php
declare(strict_types=1);

namespace LowCode\Traits;

use LowCode\Support\JsFn;

trait HasLifeCycle
{
    /**
     * @param string $name
     * @param string $value
     * @param string|null $id
     * @return $this
     */
    protected function lifeCycle(string $name, string $value, string $id = null): static
    {
        $lifeCycles = $this->get("lifeCycles",[]);
        if (!str_starts_with($value,"()=>")&&!str_starts_with($value,"function(){")){
            $value = func($value);
        }
        $lifeCycles[$name] = JsFn::make($value, $id);
        return $this->lifeCycles($lifeCycles);
    }

    /**
     * @param string $value
     * @param string|null $id
     * @return $this
     */
    public function beforeCreate(string $value, string $id = null): static
    {
        return $this->lifeCycle("beforeCreate", $value, $id);
    }

    /**
     * @param string $value
     * @param string|null $id
     * @return $this
     */
    public function created(string $value, string $id = null): static
    {
        return $this->lifeCycle("created", $value, $id);
    }

    /**
     * @param string $value
     * @param string|null $id
     * @return $this
     */
    public function beforeMount(string $value, string $id = null): static
    {
        return $this->lifeCycle("beforeMount", $value, $id);
    }

    /**
     * @param string $value
     * @param string|null $id
     * @return $this
     */
    public function mounted(string $value, string $id = null): static
    {
        return $this->lifeCycle("mounted", $value, $id);
    }

    /**
     * @param string $value
     * @param string|null $id
     * @return $this
     */
    public function beforeUpdate(string $value, string $id = null): static
    {
        return $this->lifeCycle("beforeUpdate", $value, $id);
    }

    /**
     * @param string $value
     * @param string|null $id
     * @return $this
     */
    public function updated(string $value, string $id = null): static
    {
        return $this->lifeCycle("updated", $value, $id);
    }

    /**
     * @param string $value
     * @param string|null $id
     * @return $this
     */
    public function beforeUnmount(string $value, string $id = null): static
    {
        return $this->lifeCycle("beforeUnmount", $value, $id);
    }

    /**
     * @param string $value
     * @param string|null $id
     * @return $this
     */
    public function unmounted(string $value, string $id = null): static
    {
        return $this->lifeCycle("unmounted", $value, $id);
    }

    /**
     * @param string $value
     * @param string|null $id
     * @return $this
     */
    public function errorCaptured(string $value, string $id = null): static
    {
        return $this->lifeCycle("errorCaptured", $value, $id);
    }

    /**
     * @param string $value
     * @param string|null $id
     * @return $this
     */
    public function renderTracked(string $value, string $id = null): static
    {
        return $this->lifeCycle("renderTracked", $value, $id);
    }

    /**
     * @param string $value
     * @param string|null $id
     * @return $this
     */
    public function renderTriggered(string $value, string $id = null): static
    {
        return $this->lifeCycle("renderTriggered", $value, $id);
    }

    /**
     * @param string $value
     * @param string|null $id
     * @return $this
     */
    public function activated(string $value, string $id = null): static
    {
        return $this->lifeCycle("activated", $value, $id);
    }

    /**
     * @param string $value
     * @param string|null $id
     * @return $this
     */
    public function deactivated(string $value, string $id = null): static
    {
        return $this->lifeCycle("deactivated", $value, $id);
    }
}
